package checkStatus;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Map.Entry;

import dataStruct.DataStoreTaskBag;

public class CheckTaskIdStatus  extends Thread{
	public CheckTaskIdStatus(Queue<String> dataStoreTaskSlaverChunkQueue,
			Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap)
	{
			CheckTaskIdStatus.dataStoreTaskSlaverChunkQueue = dataStoreTaskSlaverChunkQueue;
			CheckTaskIdStatus.dataStoreTaskSlaverChunkMap = dataStoreTaskSlaverChunkMap;
			
			  Properties intervalProper = new Properties();   
			  try {   
				  intervalProper.load(new FileInputStream("conf/config.ini"));
			  } catch (IOException e1) {   
			   e1.printStackTrace();   
			  }
			  shutdownInterval = Long.parseLong(intervalProper.getProperty("shutdownInterval"));
	}
	

	public void run()
	{	
		while(true)
		{
			checkOnce();
		}
	}
	
	private void checkOnce()
	{
		for(Entry<String, DataStoreTaskBag> entry1: dataStoreTaskSlaverChunkMap.entrySet())
		{
			String key = entry1.getKey();
			DataStoreTaskBag dataStoreTaskBag = entry1.getValue();
			long startTime = dataStoreTaskBag.startTime;
			if( (startTime != 0)&& (System.currentTimeMillis() - startTime > shutdownInterval))
			{
				System.out.println("taskId = [" + key + "] has timeout by slaverId = " + entry1.getValue().slaverId);
				
				dataStoreTaskSlaverChunkQueue.add(key);
				dataStoreTaskBag.startTime = System.currentTimeMillis();
			}
		}
	}
	
	static private Queue<String> dataStoreTaskSlaverChunkQueue;
	static private Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap;
	static long shutdownInterval;
}
